package com.junki.officecontentsearch.util;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author junki
 * @date 2021/6/2 9:01
 */
@Component
public class FileUploadService {

    @Value("${junki.uploadPath}")
    private String uploadPath;

    // 异步保存用户上传的文件
    @Async
    public void asyncSaveFile(MultipartFile file, String savaName) throws IOException {
        FileUtil.writeFromStream(file.getInputStream(), new File(uploadPath + "/" + savaName));
    }

    // 同步保存用户上传的文件
    public File syncSaveFile(MultipartFile file, String savaName) throws IOException {
        return FileUtil.writeFromStream(file.getInputStream(), new File(uploadPath + "/" + savaName));
    }

}
